public class QueueNumberClass 
{
    public static void assignQueue()
    {
        integer queue=0;
        Map<Id, double> newQueue = new Map<Id, double>(); 
        for (case c : [SELECT ID,Queue_Helper_ID__r.Queue_Number__c FROM Case where status='open' AND OWNERID = '00G80000001UCHeEAO' ORDER BY priority__c DESC LIMIT 500])
        {
            
          	queue ++;
          	if(c.Queue_Helper_ID__r.Queue_Number__c != queue)
          	{
            	newQueue.put(c.id, queue);
          	}
        }
        If(newQueue.size()>0)
        {
        	List<Queue_Helper__c> updateQueue = new List<Queue_Helper__c>();
        	integer updateCount=0;

        	for (Queue_Helper__c qh : [SELECT ID, Queue_number__c, Case_ID__c FROM Queue_Helper__c where Case_ID__c in :newQueue.keySet()])
        	{   

                double newQueueNum = newQueue.get(qh.Case_ID__c);
                if ((qh.Queue_number__c != newQueueNum)&(updateCount < 75))
               	{
                    qh.Queue_number__c = newQueueNum;
                    updateQueue.add(qh);    
                    updatecount ++;             
                }

            
        	}
        	update updateQueue;
        }
    }
    
    public static void clearQueue(case[] cases)
    {

   			Map<Id, Case> ClearCases = new Map<Id, Case>();
   			List<Queue_Helper__c> updateQueue = new List<Queue_Helper__c>();
   			for (Case c:cases)
         	{
         		If(c.OwnerId != '00G80000001UCHeEAO' & c.Queue_Helper_ID__r.Queue_Number__c > 0)
   				{
   					ClearCases.put(c.id, c);
   				}	
         	}
         	If(ClearCases.size()>0)
         	{
         		integer updateCount=0;
   				for (Queue_Helper__c qh : [SELECT ID, Queue_number__c, Case_ID__c FROM Queue_Helper__c where Case_ID__c in :clearCases.keySet()])
   				{
                	if (updateCount < 99)
                	{
                    	qh.Queue_number__c = 0;
                    	updateQueue.add(qh);    
                    	updatecount ++;             
                	}   				
   				}

        		update updateQueue; 
         	} 					
  
    }
    
    //This method creates a new queue helper object every time a case is inserted
    public static void NewQueueHelper(case[] cases)
    {
	   	//create list to hold new queue helpers
		List<Queue_Helper__c> Queue_Helpers = new List<Queue_Helper__c>();
		//loop through newly inserted cases
		for (Case c: cases) 
		{
			Queue_Helpers.add(new Queue_Helper__c(
				Case_ID__c = c.ID));
		}
		insert Queue_Helpers;
    }
    
	public static void LinkQueueHelperCase(Queue_Helper__c[] QueueHelpers)
	{
		//create map to associate queue helper id's to case id's
	    Map<Id, Queue_Helper__c> caseQueueHelperMap = new Map<Id, Queue_Helper__c>();
		for (Integer i = 0; i < QueueHelpers.size(); i++) 
		{
			caseQueueHelperMap.put(QueueHelpers[i].Case_ID__c,QueueHelpers[i]);
		}
	            
		//create list to hold updated cases
		List<Case> Update_Cases = new List<case>();
		for (Case C : [SELECT ID, Queue_Helper_ID__c FROM Case where ID in :caseQueueHelperMap.keySet()]) 
		{
			Queue_Helper__c queue = caseQueueHelperMap.get(c.Id);
			c.Queue_Helper_ID__c = queue.ID;
			update_Cases.add(c);
		}
	
		update Update_Cases;
	}
}